layui.config({
  base: "js/"
}).use(['form', 'layer', 'jquery', 'laypage'], function () {
  var form = layui.form(),
    layer = parent.layer === undefined ? layui.layer : parent.layer,
    laypage = layui.laypage,
    $ = layui.jquery;

  //加载页面数据
  var newsData = '';
  $.get("/api/user/all", function (data) {
    console.log(data);
    render(eval(data));
  });

  $(".add").click(function () {
    var index = layui.layer.open({
      title: "添加用户",
      type: 2,
      content: "add.html",
      area: ['60%', '50%'],
      success: function (layero, index) {
        setTimeout(function () {
          layui.layer.tips('点击此处返回课程列表', '.layui-layer-setwin .layui-layer-close', {
            tips: 3
          });
        }, 500)
      }
    });
  });

  //查询
  $(".search_btn").click(function () {
    var newArray = [];
    var index = layer.msg('查询中，请稍候', {icon: 16, time: 3000, shade: 0.8});
    $.ajax({
      url: "/api/user?User_id=" + $(".search_input").val() + "&type=" + $("#User_identity").val(),
      type: "get",
      dataType: "json",
      success: function (data) {
        layer.close(index);
        render(eval(data));
      }
    });
  });


  $("body").on("click", ".del", function () {  //删除
    var log_id = $(this).attr("data-user-id");
    layer.confirm('确定删除此用户？', {icon: 3, title: '提示信息'}, function (index) {
      $.ajax({
        url: "/api/user",
        type: "DELETE",
        data: {log_id: log_id},
        dataType: "json",
        success: function (data) {
          layer.msg("删除成功！");
          window.location.reload();
        },
        error: function (data) {
          layer.msg("删除失败!");
        }
      });
    });
  });

  function render(that) {
    //渲染数据
    function renderDate(data, curr) {
      var dataHtml = '';
      if (!that) {
        currData = newsData.concat().splice(curr * nums - nums, nums);
      } else {
        currData = that.concat().splice(curr * nums - nums, nums);
      }
      if (currData.length !== 0) {
        for (var i = 0; i < currData.length; i++) {
          dataHtml += '<tr>'
            + '<td>' + currData[i].User_id + '</td>';
          if (currData[i].User_identity === 0)
            dataHtml += '<td>管理员</td>';
          else if (currData[i].User_identity === 1)
            dataHtml += '<td>教师</td>';
          else if (currData[i].User_identity === 2)
            dataHtml += '<td>学生</td>';
          dataHtml += '<td>'
            + '<a class="layui-btn layui-btn-danger layui-btn-mini del" data-user-id="' + currData[i].User_id + '" data-user-type="' + currData[i].User_identity + '"><i class="layui-icon">&#xe640;</i> 删除</a>'
            + '</td>'
            + '</tr>';
        }
      } else {
        dataHtml = '<tr><td colspan="3">暂无数据</td></tr>';
      }
      return dataHtml;
    }

    //分页
    var nums = 10; //每页出现的数据量
    if (that) {
      newsData = that;
    }
    laypage({
      cont: "page",
      pages: Math.ceil(newsData.length / nums),
      jump: function (obj) {
        $(".content").html(renderDate(newsData, obj.curr));
        form.render();
      }
    })
  }
})
